﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;
using DBService.ViewDatas;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("bg_search").Visible = false;

        SearchText.Attributes.Add("onkeypress", "return clickButton(event,'" + SearchID.ClientID + "')");

        BindReviewRepeater();
        BindNewCompanyRepeater();
        //BindNewPosterRepeater();
        BindTopMembersRepeater();
        TopCompanies_DataBound();
    }

    public void LatestReview_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (ReviewInfo)e.Item.DataItem;
        string reviewText = item.review.Length > 500 ? item.review.Substring(0, 155) : item.review;
        var diff = (DateTime.Now - item.createDate);
        var timeString = diff.TotalMinutes < 60 ? diff.Minutes + " წუთის წინ..." : item.createDate.ToShortDateString();

        string avatar = "";
        if (!item.UserFB.HasValue)
        {
            avatar = string.IsNullOrEmpty(item.avatar) ? "images/no-avatar.jpeg" : "upload/" + item.avatar;
        }
        else
        {
            avatar = String.Format("http://graph.facebook.com/{0}/picture?width=50&height=50", item.UserFB.ToString());
        }

        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("LatestReviewItem");
            lit.Text = "<div class=\"late_box\"><div class=\"img_news\">"
                                + "<img src=\"" + avatar + "\" width=\"50\" height=\"50\" alt=\"\"></div>"
                            + "<div class=\"member_txt1\"><a href=\"UserProfile.aspx?id=" + item.userId + "\">" + item.userFullName + "</a></div>&rarr;<span><a href=\"SingleCompany.aspx?id=" + item.CompanyID + "\">" + " " + item.companyName + "&nbsp" + "</a></span>"
                            + reviewText
                            + "<div class=\"seconds_ago\">"
                            + timeString
                            + "</div>"
                            + "</div>";
        }
    }

    public void NewCompanies_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("ka-GE");
        var item = (Company)e.Item.DataItem;
        var logopath = "upload/" + (String.IsNullOrEmpty(item.Logo) ? "no-company-logo.jpg" : item.Logo);
        var CompanyName = item.Name.Count() > 14 ? item.Name.Substring(0, 14) + "..." : item.Name;
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("NewCompanyItem");
            lit.Text = "<div class=\"member_one\">"
                        + "<div class=\"mem_box\">"
                            + "<a href=\"singlecompany.aspx?id=" + item.CompanyID + "\">"
                                + "<img src=\"" + logopath + "\" width=\"50\" height=\"50\" ></a>"
                        + "</div>"
                       + "<div class=\"member_txt\">"
                            + "<a href=SingleCompany.aspx?id=" + item.CompanyID + ">" + CompanyName + "<br>"
                            + "</a><span><a href=\"singlecompany.aspx?id=" + item.CompanyID + "\">განიხილე</a></span>"
                        + "</div>"
                    + "</div>";
        }
    }

    public void NewPosters_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("ka-GE");
        var item = (Poster)e.Item.DataItem;
        var logopath = "upload/" + (String.IsNullOrEmpty(item.Logo) ? "no-company-logo.jpg" : item.Logo);
        var CompanyName = item.Company.Name.Count() > 14 ? item.Company.Name.Substring(0, 14) + "..." : "";
        var diff = (DateTime.Now - item.CreateDate.Value);
        var timeString = diff.TotalMinutes < 60 ? diff.Minutes + " წუთის წინ..." : item.CreateDate.Value.ToShortDateString();
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("NewPosterItem");
            lit.Text = "<div class=\"late_box\"><div class=\"img_news\">"
                        + "<img src=\"" + logopath + "\" width=\"50\" height=\"50\" alt=\"\"></div>"
                    + "<div><a href=\"SingleCompnay.aspx?id=" + item.CompanyID + "\">" + item.Company.Name + "</a></div> "
                    + item.Title
                    + "<div class=\"seconds_ago\">"
                    + timeString
                    + "</div>"
                    + "</div>";
        }
    }

    public void TopMembers_DataBound(Object Sender, RepeaterItemEventArgs e)
    {
        var item = (TopMembers)e.Item.DataItem;

        string avatar = "";
        if (!item.UserFB.HasValue)
        {
            avatar = string.IsNullOrEmpty(item.avatar) ? "images/no-avatar.jpeg" : "upload/" + item.avatar;
        }
        else
        {
            avatar = String.Format("http://graph.facebook.com/{0}/picture?width=50&height=50", item.UserFB.ToString());
        }
        var like = item.Like ?? 0;
        if (item != null)
        {
            var lit = (Literal)e.Item.FindControl("TopMemberItem");
            lit.Text = "<div class=\"member_one\">"
                            + "<div class=\"mem_box\">"
                                + "<a href=\"#\">"
                                    + "<img src=\"" + avatar + "\" width=\"50\" height=\"50\"></a>"
                            + "</div>"
                            + "<div class=\"member_txt\">"
                                + "<a href=\"UserProfile.aspx?id=" + item.UserId + "\">" + item.FirstName + "<br>"
                                    + item.LastName + "<br>"
                               + "</a><span>" + " რეიტინგი" + "&rarr;" + item.UserRating + "</span>"
                            + "</div>"
                        + "</div>";
        }
    }

    public void TopCompanies_DataBound()
    {
        var service = new ModelAdapter();
        List<int> list = service.GetTopCategoryIDs();
        if (list == null || list.Count == 0)
        {
            //Scriptt.Text = "<script>$(document).ready(function(){ $('.late_box_block').css('background-color','White');});</script>";
            return;
        }
        var companies_cate = service.GetTopCompanies(list[0]);
        var companies_cate1 = service.GetTopCompanies(list[1]);
        string topCompaniesItem1 = "";
        string topCompaniesItem2 = "";
        string literal1 = "";
        string literal2 = "";

        for (int i = 0; i < companies_cate.Count; i++)
        {
            topCompaniesItem1 += "<div class=\"num_b\"> <a href=\"SingleCompany.aspx?id=" + companies_cate[i].CompanyID + "\">" + (i + 1) + "." + companies_cate[i].Name + "</a></div>";
            if (i + 1 == companies_cate.Count)
            {
                var logopath = "upload/" + (String.IsNullOrEmpty(companies_cate[0].Logo) ? "no-company-logo.jpg" : companies_cate[0].Logo);

                literal1 = "<div class=\"topcomp_box\">"
                       + "<div class=\"top_border\">"
                           + "<img src=\"" + logopath + "\" width=\"83\" height=\"49\" alt=\"\">"
                       + "</div>"
                       + "<div class=\"rest_right\">"
                       + "<h4>" + companies_cate[0].Title + "</h4>"
                       + "<div class=\"numer\">"
                         + topCompaniesItem1
                       + "</div><!--numer-->"
                       + "</div>"
                       + "<div class=\"clear\"></div>"
                   + "</div>";
            }
        }
        for (int i = 0; i < companies_cate1.Count; i++)
        {
            topCompaniesItem2 += "<div class=\"num_b\"> <a href=\"SingleCompany.aspx?id=" + companies_cate1[i].CompanyID + "\">" + (i + 1) + "." + companies_cate1[i].Name + "</a></div>";

            if (i + 1 == companies_cate1.Count)
            {
                var logopath = "upload/" + (String.IsNullOrEmpty(companies_cate1[0].Logo) ? "no-company-logo.jpg" : companies_cate1[0].Logo);
                literal2 = "<div class=\"topcomp_box\">"
                    + "<div class=\"top_border\">"
                        + "<img src=\"" + logopath + "\" width=\"83\" height=\"49\" alt=\"\">"
                    + "</div>"
                    + "<div class=\"rest_right\">"
                    + "<h4>" + companies_cate1[0].Title + "</h4>"
                    + "<div class=\"numer\">"
                        + topCompaniesItem2
                    + "</div><!--numer-->"
                    + "</div>"
                    + "<div class=\"clear\"></div>"
                + "</div>";
            }
        }

        if (companies_cate.Count == 0 && companies_cate1.Count != 0)
        {
            TopCompaniesItemfirst.Text = literal2;
        }
        else if (companies_cate.Count != 0 && companies_cate1.Count == 0)
        {
            TopCompaniesItemSecond.Text = literal1;
        }
        else if (companies_cate1.FirstOrDefault() != null && companies_cate1.FirstOrDefault().SumRateing > companies_cate.FirstOrDefault().SumRateing)
        {
            TopCompaniesItemfirst.Text = literal2;
            TopCompaniesItemSecond.Text = literal1;
        }
        else
        {
            TopCompaniesItemfirst.Text = literal1;
            TopCompaniesItemSecond.Text = literal2;
        }
    }

    private void BindTopMembersRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetTopMembersForRatings2().OrderByDescending(c => c.UserRating).Take(50).OrderBy(c => Guid.NewGuid()).Take(8).OrderByDescending(c => c.UserRating).ToList();
        TopMemberRepeater.DataSource = coll;
        TopMemberRepeater.DataBind();
    }

    private void BindNewCompanyRepeater()
    {
        var service = new ModelAdapter();
        var coll = service.GetNewCompaneis();
        NewCompanyRepeater.DataSource = coll;
        NewCompanyRepeater.DataBind();
    }

    //private void BindNewPosterRepeater()
    //{
    //    var service = new ModelAdapter();
    //    var coll = service.GetNewPosters();
    //    NewPosterRepeater.DataSource = coll;
    //    NewPosterRepeater.DataBind();
    //}

    private void BindReviewRepeater()
    {
        var userID = (int?)Session["UserID"];
        var service = new ModelAdapter();
        var coll = service.GetLatestReviewForHomePage(20, userID).ToList();
        LatestReview.DataSource = coll;
        LatestReview.DataBind();
    }

    protected void OnSearch(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(SearchText.Text))
        {
            Session["keyword"] = SearchText.Text;
            Response.Redirect("SearchResult.aspx");
        }
    }
}
